#ifndef SERVICE_MANAGER_H
#define SERVICE_MANAGER_H


#include <thread>
#include <unordered_map>
#include <vector>
#include <mutex>
#include <memory>
#include "Service.h"
#include "LoadBalance.h"
#include <functional>


#include <ppconsul/agent.h>
#include <ppconsul/consul.h>
#include <ppconsul/health.h>
#include <thread>

#include <sys/ioctl.h>
#include <net/if.h>
#include <unistd.h>
#include <arpa/inet.h>
using namespace ppconsul;


class ServiceManager{
public:

    static ServiceManager& getInstance();
    ~ServiceManager() = default;

    void start();
    void stop();
    void addServiceInstance(std::string name, ServiceItem service, int ttl_time);
    void removeServiceInstance(std::string name, ServiceItem service);
    ServiceItem getNextService(std::string name);
    void syncServices();  // 同步所有服务
    void syncService(std::string service_name); //同步一个服务
    void setLoadBalanceStrategy(const std::shared_ptr<LoadBalanceStrategy>& strategy);

    //consul配置
    void setConsulConfig(std::string address, std::string token);
private:

    std::unordered_map<std::string, Service> services_map;
    std::shared_ptr<LoadBalanceStrategy> strategy;

    //单例模式
    static std::unique_ptr<ServiceManager> instance;
    static std::mutex singleton_mutex;
    ServiceManager();


    //consul配置
    std::shared_ptr<Consul> consul_client;
    std::string consul_server_host;
    std::shared_ptr<agent::Agent> agent;
    std::string consul_token;

};

#endif